<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    protected $fillable = ['title', 'content', 'status', 'rank', 'category_id', 'crawler_article_id'];

    public function pictuer()
    {
        return $this->hasMany('App\Models\Pictuer');
    }

    public function category()
    {
        return $this->belongsTo('App\Models\Category');
    }

    public function crawler()
    {
        return $this->belongsTo('App\Models\CrawlerArticle', 'crawler_article_id');
    }

    public function clearErrorArticle()
    {
        return $this->whereDoesntHave('pictuer')->update([
            'status' => -1
        ]);
    }

    public function next_article()
    {
        return $this->where([['status', '>', -1], ['category_id', $this->category_id], ['created_at', '<', $this->created_at ]])->latest()->first();
    }

    public function prev_article()
    {
        return $this->where([['status', '>', -1], ['category_id', $this->category_id], ['created_at', '>', $this->created_at ]])->oldest()->first();
    }
}
